package com.orangelife.service.propFeeOffsetInfo.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.orangelife.common.MybaitsGenericDaoSubLogic;
import com.orangelife.exception.AppBizException;
import com.orangelife.service.propFeeOffsetInfo.IPropFeeOffsetInfoService;
import com.orangelife.service.propFeeOffsetInfo.model.PropFeeOffsetInfoDto;
import com.orangelife.setting.constant.CodeConst;
import com.orangelife.setting.constant.InfoConst;
import com.orangelife.setting.constant.TypeConst;
import com.orangelife.utils.StringUtils;
import com.orangelife.utils.UUIDGenerator;

/*****************************************************************
 * Copyright(C) ORANGELIFE Corporation CodeConst.BIS_CODE_68001015. All Rights
 * Reserved.
 *****************************************************************/
/*
 * ============================================================== 功能名
 * PropFeeOffsetInfo类的 service层的增删改查
 * 
 * @author @author zhr
 * 
 * @version 1.1.0
 * 
 * @date 2016-11-24 14:18:55
 * ==============================================================
 */
@Service("propFeeOffsetInfoService")
public class PropFeeOffsetInfoServiceImpl extends MybaitsGenericDaoSubLogic
		implements IPropFeeOffsetInfoService {
	private static Logger logger = Logger
			.getLogger(PropFeeOffsetInfoServiceImpl.class);

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PropFeeOffsetInfoServiceImpl.logger = logger;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int addPropFeeOffsetInfo(PropFeeOffsetInfoDto propFeeOffsetInfo)
			throws AppBizException {

		Assert.notNull(propFeeOffsetInfo);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/

		if (!StringUtils.isBlank(propFeeOffsetInfo.getFeeDate())) {
			if (propFeeOffsetInfo.getFeeDate().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "所属月份长度超出");
			}
		}

		if (propFeeOffsetInfo.getOffsetID() == null) {
			propFeeOffsetInfo.setOffsetID(UUIDGenerator.nextOrderNum());

		}

		if (StringUtils.isBlank(propFeeOffsetInfo.getStatus())) {
			propFeeOffsetInfo.setStatus("1");
		}

		propFeeOffsetInfo.setItemStat(TypeConst.FLAG_ON);
		propFeeOffsetInfo.setTscreate(new Date());
		propFeeOffsetInfo.setTsedit(new Date());
		return this.getSqlSession().insert(
				"PropFeeOffsetInfoMapper.insertSelective", propFeeOffsetInfo);
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int editPropFeeOffsetInfo(PropFeeOffsetInfoDto propFeeOffsetInfo)
			throws AppBizException {

		Assert.notNull(propFeeOffsetInfo);
		/************************************************
		 * 判断输入参数的长度以及是否能为空
		 ************************************************/
		if (StringUtils.isBlank(propFeeOffsetInfo.getOffsetID())) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		PropFeeOffsetInfoDto oldPropFeeOffsetInfo = this
				.findPropFeeOffsetInfoByID(propFeeOffsetInfo.getOffsetID());

		if (!StringUtils.isBlank(propFeeOffsetInfo.getFeeDate())) {
			if (propFeeOffsetInfo.getFeeDate().length() > 16) {
				throw new AppBizException(CodeConst.SYS_CODE_1002, "所属月份长度超出");
			}
		}

		if (oldPropFeeOffsetInfo == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		propFeeOffsetInfo.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeOffsetInfoMapper.updateByPrimaryKeySelective",
				propFeeOffsetInfo);
	}

	@Override
	public PropFeeOffsetInfoDto findPropFeeOffsetInfoByID(
			String propFeeOffsetInfoID) throws AppBizException {
		if (StringUtils.isBlank(propFeeOffsetInfoID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeOffsetInfoDto oldPropFeeOffsetInfo = this.getSqlSession()
				.selectOne("PropFeeOffsetInfoSearchMapper.selectByPrimaryKey",
						propFeeOffsetInfoID);
		if (oldPropFeeOffsetInfo == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeOffsetInfo.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeOffsetInfo;
	}

	@Override
	@Transactional(rollbackFor = { AppBizException.class, Exception.class,
			RuntimeException.class })
	public int deletePropFeeOffsetInfoByLogic(String propFeeOffsetInfoID)
			throws AppBizException {

		if (StringUtils.isBlank(propFeeOffsetInfoID)) {
			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}
		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeOffsetInfoDto oldPropFeeOffsetInfo = this
				.findPropFeeOffsetInfoByID(propFeeOffsetInfoID);

		oldPropFeeOffsetInfo.setItemStat(TypeConst.FLAG_OFF);
		oldPropFeeOffsetInfo.setTsedit(new Date());
		return this.getSqlSession().update(
				"PropFeeOffsetInfoMapper.updateByPrimaryKeySelective",
				oldPropFeeOffsetInfo);
	}

	@Override
	public List<Map<String, Object>> findPropFeeOffsetInfoByPage(String commID,
			String searchType, String startTime, String endTime,
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		if (!StringUtils.isBlank(startTime)) {
			map.put("startTime", Long.parseLong(startTime) / 1000);
		}
		if (!StringUtils.isBlank(endTime)) {
			map.put("endTime", Long.parseLong(endTime) / 1000);
		}

		map.put("commID", commID);
		if (!StringUtils.isBlank(searchType)) {
			map.put("searchType", String.valueOf(searchType));
		}
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeOffsetInfoSearchMapper.findAllPropFeeOffsetInfo", map);
		return list;
	}

	@Override
	public List<PropFeeOffsetInfoDto> getPropFeeOffsetInfoByPage(
			String tsCreate, String search, int pageNo, int pageSize)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("tsCreate", tsCreate);
		map.put("search", search);
		PageHelper.startPage(pageNo, pageSize);
		List<PropFeeOffsetInfoDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeOffsetInfoSearchMapper.getAllPropFeeOffsetInfoDto",
						map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectPropFeeOffsetInfoByRelevantID(
			String offsetID, String commID, String apartmentID,
			String feeDetailID, String feeTypeID) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("offsetID", offsetID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);
		map.put("feeDetailID", feeDetailID);
		map.put("feeTypeID", feeTypeID);
		map.put("feeTypeID", feeTypeID);

		List<Map<String, Object>> list = this.getSqlSession().selectList(

		"PropFeeOffsetInfoSearchMapper.selectPropFeeOffsetInfoDtoByRelevantID",
				map);
		return list;
	}

	@Override
	public List<PropFeeOffsetInfoDto> findPropFeeOffsetInfoByRelevantID(
			String offsetID, String commID, String apartmentID,
			String feeDetailID, String feeTypeID, String offsetType,
			int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("offsetID", offsetID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);
		map.put("feeDetailID", feeDetailID);
		map.put("feeTypeID", feeTypeID);
		map.put("offsetType", offsetType);
		PageHelper.startPage(pageNo, pageSize);

		List<PropFeeOffsetInfoDto> list = this
				.getSqlSession()
				.selectList(
						"PropFeeOffsetInfoSearchMapper.findPropFeeOffsetInfoDtoByRelevantID",
						map);
		return list;
	}

	@Override
	public List<PropFeeOffsetInfoDto> bauthfindPropFeeOffsetInfo(
			List<String> propFeeOffsetInfoIDs) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<PropFeeOffsetInfoDto> list = this.getSqlSession().selectList(
				"PropFeeOffsetInfoSearchMapper.batchFindPropFeeOffsetInfo",
				propFeeOffsetInfoIDs);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeOffsetInfoByMap(
			Map<String, Object> propFeeOffsetInfo) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/

		if (!StringUtils.isBlank(StringUtils.toString(propFeeOffsetInfo
				.get("ispage")))) {

			propFeeOffsetInfo.put("pageIndex", StringUtils.toString(
					propFeeOffsetInfo.get("pageIndex"), "1"));
			propFeeOffsetInfo.put("pageSize", StringUtils.toString(
					propFeeOffsetInfo.get("pageSize"), "10"));

			PageHelper.startPage(Integer.parseInt(propFeeOffsetInfo.get(
					"pageIndex").toString()), Integer
					.parseInt(propFeeOffsetInfo.get("pageSize").toString()));
		}

		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeOffsetInfoSearchMapper.batchFindPropFeeOffsetInfoByMap",
						propFeeOffsetInfo);
		return list;
	}

	@Override
	public void clearCache() throws AppBizException {
		System.out.println("清除缓存成功");
	}

	@Override
	public List<Map<String, Object>> getOffDetail(
			Map<String, Object> propFeeOffsetInfo) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this.getSqlSession()
				.selectList("PropFeeOffsetInfoSearchMapper.getOffDetail",
						propFeeOffsetInfo);
		return list;
	}

	@Override
	public Map<String, Object> getOffTime(Map<String, Object> propFeeOffsetInfo)
			throws AppBizException {

		if (propFeeOffsetInfo.get("apartmentID") != null) {
			List<String> feeDates = this.getOffFeeDate(propFeeOffsetInfo.get(
					"apartmentID").toString());

			propFeeOffsetInfo.put("feeDates", feeDates);
		}
		return this.getSqlSession().selectOne(
				"PropFeeOffsetInfoSearchMapper.getOffTime", propFeeOffsetInfo);

	}

	@Override
	public List<String> getOffFeeDate(String apartmentID)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("apartmentID", apartmentID);

		List<String> list = new ArrayList<String>();
		list = this.getSqlSession().selectList(
				"PropFeeOffsetInfoSearchMapper.getOffFeeDate", map);
		if (list.size() <= 0) {
			list.add("-1");
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthSearchPropFeeDetails(
			Map<String, Object> propFeeDetail) throws AppBizException {
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeOffsetInfoSearchMapper.bauthSearchPropFeeDetails",
				propFeeDetail);
		return list;
	}

	@Override
	public List<Map<String, Object>> synchpreBillOffData(Map<String, Object> map)
			throws AppBizException {

		/************************************************
		 * 设置输入参数
		 ************************************************/
		if (map.containsKey("tsedit")) {
			if (map.get("tsedit") != null) {
				if (!StringUtils.isInt(map.get("tsedit").toString())) {
					throw new AppBizException(2, "参数类型错误");
				}
				map.put("tsedit",
						Long.parseLong(map.get("tsedit").toString()) / 1000);
			}
		}
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeOffsetInfoSearchMapper.synchpreBillOffData", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> selectOffsetInfoForApp(String status,
			String offsetID, String commID, String apartmentID,
			String feeDetailID, String feeTypeID, String offsetType,
			int pageNo, int pageSize) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("offsetID", offsetID);
		map.put("commID", commID);
		map.put("apartmentID", apartmentID);
		map.put("feeDetailID", feeDetailID);
		map.put("feeTypeID", feeTypeID);
		if (!StringUtils.isBlank(offsetType)) {

			String offsetTypes[] = offsetType.split(",");
			map.put("offsetTypes", Arrays.asList(offsetTypes));

		}
		map.put("status", status);
		PageHelper.startPage(pageNo, pageSize);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeOffsetInfoSearchMapper.findPropFeeOffsetForApp", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> getOffsetInfoByMap(
			Map<String, Object> params) throws AppBizException {
		if (!StringUtils.isBlank(StringUtils.toString(params.get("ispage")))) {

			PageHelper.startPage(
					Integer.parseInt(params.get("pageIndex").toString()),
					Integer.parseInt(params.get("pageSize").toString()));
		}

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeOffsetInfoSearchMapper.selectSimpleOffsetInfo", params);
		return list;
	}

	@Override
	public PropFeeOffsetInfoDto findMorePropFeeOffsetInfoByID(
			String propFeeOffsetInfoID) throws AppBizException {
		if (StringUtils.isBlank(propFeeOffsetInfoID)) {

			throw new AppBizException(CodeConst.SYS_CODE_1001, "ID不可为空");
		}

		/************************************************
		 * 判断实体是否存在
		 ************************************************/
		PropFeeOffsetInfoDto oldPropFeeOffsetInfo = this.getSqlSession()
				.selectOne("PropFeeOffsetInfoSearchMapper.selectByPrimaryKey2",
						propFeeOffsetInfoID);
		if (oldPropFeeOffsetInfo == null) {
			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}

		if (TypeConst.FLAG_OFF.equals(oldPropFeeOffsetInfo.getItemStat())) {

			throw new AppBizException(CodeConst.BIS_CODE_68001,
					InfoConst.BIS_INFO_68001);
		}
		return oldPropFeeOffsetInfo;
	}

	@Override
	public List<Map<String, Object>> getOffsetInfo(String orderID, String status)
			throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderID", orderID);
		map.put("status", status);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeOffsetInfoSearchMapper.getOffsetInfo", map);
		return list;
	}

	@Override
	public List<Map<String, Object>> bauthfindPropFeeOffset(
			Map<String, Object> propFeeOffsetInfo) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		List<Map<String, Object>> list = this
				.getSqlSession()
				.selectList(
						"PropFeeOffsetInfoSearchMapper.batchFindPropFeeOffsetInfoByMap",
						propFeeOffsetInfo);
		return list;
	}

	@Override
	public List<Map<String, Object>> findOffCount(
			Map<String, Object> propFeeOffsetInfo) throws AppBizException {
		List<Map<String, Object>> list = this.getSqlSession()
				.selectList("PropFeeOffsetInfoSearchMapper.findOffCount",
						propFeeOffsetInfo);
		return list;
	}

	@Override
	public List<Map<String, Object>> findRectCount(
			Map<String, Object> propFeeOffsetInfo) throws AppBizException {
		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeOffsetInfoSearchMapper.findRectCount",
				propFeeOffsetInfo);
		return list;
	}

	@Override
	public List<Map<String, Object>> getOffsetInfos(String orderID,
			String status) throws AppBizException {
		/************************************************
		 * 设置输入参数
		 ************************************************/
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("orderID", orderID);
		map.put("status", status);

		List<Map<String, Object>> list = this.getSqlSession().selectList(
				"PropFeeOffsetInfoSearchMapper.getOffsetInfos", map);
		return list;
	}

}